package com.weedsdream.app.compose

import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.weedsdream.app.function.fullView
import com.weedsdream.app.struct.FileOpe
import com.weedsdream.app.struct.SpOpe
import com.weedsdream.app.struct.drafts

/**橡皮擦选中时*/
@Composable
fun RowScope.Eraser(test: Boolean = true) {
    Row(
        Modifier.align(Alignment.CenterVertically), verticalAlignment = Alignment.CenterVertically
    ) {
        if (test || bgImg != null) Text("取消背景", textClick { bgImg = null }, Color(0xffc57326))
        if (test || drafts.isNotEmpty()) Text("清空笔迹", textClick {
            SpOpe.read()?.let {
                SpOpe.delete()//删除临时文件名
                FileOpe.deleteProject(it)//删除临时文件
            }
            drafts.clear()
            fullView()
        }, Color.Red)
    }
}

/**清除按钮点击时*/
private fun textClick(click: () -> Unit) = Modifier
    .pointerInput(Unit) {
        detectTapGestures {
            click()
            curTool = ToolType.笔
        }
    }
    .padding(10.dp)

@Preview
@Composable
private fun Test() {
    Row {
        Eraser()
    }
}